<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
    <title>发布</title> 	
    	<link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.11/summernote.css" rel="stylesheet">
      	<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
    <link rel="stylesheet" href="css/base.css" />
    <link rel="stylesheet" href="css/set.css" />
    <link rel="stylesheet" href="css/all.css" />
   
	  
</head>
<body>
<div>
    <div class="heads">
        <span href="javascript:;" onclick="go()"><img src="img/zuojiantou.png" alt="" class="arrows"></span>
        <h1 class="titles l-h">发布</h1>
        <a href=""><div class="certain">确定</div></a>
    </div>
    <div class="middle">
        <form action="">
            <div class="resetUpwd">
                <input type="text" placeholder="添加广告题目" class="resetUpwd-input" id="publish-title"/>
            </div>
            <!--<textarea class="describe" placeholder="描述广告详细内容"></textarea>-->
            <div id="summernote"></div>
        </form>
        <div class="picture p-r">
            
            <img src="img/tianjia.png" alt="" class="tianjia pictures" id="publish-img">
            <input type="file" accept="image/jpg,image/jpeg,image/png" name="file" onChange="selectImg(this,'publish-img')" class="publish-file">
        </div>
       <form action="">
            <div class="resetUpwd-one">
                <span class="money">总金额</span>
                <input type="text" placeholder="0.00" class="resetUpwd-in" id="publish-envelopesNum"/>
                <span class="moneys">元</span>
            </div>
            <div class="resetUpwd-two">
                <span class="money">红包个数</span>
                <input type="text" placeholder="填写个数" class="resetUpwd-in" id="publish-totalMoney"/>
                <span class="moneys">个</span>
            </div>
        </form>
    </div>
    <div type="button" class="submit" >
        <button class="submits">提交</button>
    </div>
</div>
 <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
	  <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> 
	  
	  <script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.11/summernote.js"></script>


<!--<script src="js/jquery.js"></script>-->
<script src="js/x_rem.js"></script>
<script src="js/secondLevel.js"></script>
<script src="js/all.js"></script>
<script>
	//判断是否登录
	$.ajax({
			type:"post",
			url:Url+"/user/isLog",
			xhrFields:{
				withCredentials:true
			},
			data:{
					
			},
			success:((data)=>{
				console.log(data);
				if(data){
					popup("不登录不能发布");
//					location.href="login.html";
				}
			}),
			error:((err)=>{
				console.log(err);
			})
		});
		
	
	
		$(document).ready(function() {
			$('#summernote').summernote({
		        placeholder: '请输入内容',
		        tabsize: 1,
		        height: 150,
	    	});
		});
	
	
	var picture = "";
		//换头像
	function selectImg(file,id) {
			 if (!file.files || !file.files[0]){
	            return;
	        }
 
	        var reader = new FileReader();
	        reader.onload = function (evt) {
	           var replaceSrc = evt.target.result;
	            //更换cropper的图片
	           // $('#tailoringImg').cropper('replace', replaceSrc,false);//默认false，适应高度，不失真
				$("#"+id).prop("src",replaceSrc);//显示为图片的形式
  					
  				 
  				 $.ajax({
					type:"post",
					url:Url+"/base64/upload",
					xhrFields:{
						withCredentials:true
					},
					data:{
						base64:replaceSrc
					},
					success:((data)=>{
						console.log(data);
						if(data.success){
							picture = data.message;
						}else{
							popup(data.message);
						}
						
					}),
					error:((err)=>{
						console.log(err);
					})
				});
  				 
	        }
	        reader.readAsDataURL(file.files[0]);
	       // base64Images.pust();	
 
		}
		
	
	
	
	 
	 
		var title = $("#publish-title");   //添加广告题目
		 
		var envelopesNum = $("#publish-envelopesNum");      //总金额 
		var totalMoney = $("#publish-totalMoney");          //红包个数
		var picture;                          //图片
		
		
//	if(vali(envelopesNum,/^[0-9]+([.]{1}[0-9]{1,2})?$/,"总金额小数/不能超过两位数/不能为空")
		
		
		$(".submits").click(()=>{
 
			
			
			if(title.val() == ""){
				popup("标题不能为空");
			}else if(!vali(envelopesNum,/^[0-9]+([.]{1}[0-9]{1,2})?$/,"总金额小数/不能超过两位数/不能为空")){
				
			}else if(picture == ""){
				popup("图片没有上传");
			}else if(!vali(totalMoney,/^[0-9]*[1-9][0-9]*$/,"红包个数必须为整数")){
				
			}else{
				var markupStr = $('#summernote').summernote('code');
//				console.log(markupStr);
  			
  				
  				 console.log(picture);
	  			$.ajax({
					type:"post",
					url: Url+"/browseTask/addBrowseTask",
					xhrFields:{
						withCredentials:true
					},
					data:{
						"title":title.val(),
						"body" : markupStr,
						"picture":picture,
						"totalMoney" : envelopesNum.val(),
						"envelopesNum" : totalMoney.val()
					},
					success:((data)=>{
						console.log(data);
						popup(data.message)
					}),
					error:((err)=>{
						console.log(err);
					})
				});
 
			}
		
			
			
			
			
			
			
		})
		
		
		
			
		
		
		
 
	
	
	
</script>
</body>
</html>